//
//  MultiChoose2ViewController.m
//  DebugDemo
//
//  Created by XFB on 2019/1/18.
//  Copyright © 2019年 ZhouCheng. All rights reserved.
//

#import "MultiChoose2ViewController.h"
#import "ChooseCell.h"
#import "ChooseModel.h"

@interface MultiChoose2ViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) NSMutableArray *selectIndexPaths;

@end

@implementation MultiChoose2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupBasicSet];
    [self setupUI];
}

- (void)setupBasicSet
{
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"多选2";
    self.selectIndexPaths = @[].mutableCopy;
    
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemDidClick:)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
}

- (void)rightBarButtonItemDidClick:(UIBarButtonItem *)item
{
    if (self.selectIndexPaths.count > 0) {
        
        NSMutableString *themeString = [[NSMutableString alloc] init];
        for (ChooseModel *chooseModel in self.selectIndexPaths) {
            [themeString appendString:chooseModel.theme];
            [themeString appendString:@"、"];
        }
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"标题" message:themeString preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alertVC addAction:okAction];
        [alertVC addAction:cancelAction];
        [self.navigationController presentViewController:alertVC animated:YES completion:nil];
        
    } else {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"标题" message:@"请至少选择一项" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        }];
        [alertVC addAction:okAction];
        [alertVC addAction:cancelAction];
        [self.navigationController presentViewController:alertVC animated:YES completion:nil];
    }
}

- (void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView reloadData];
}


#pragma mark --- <UITableViewDataSource>
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseCell"];
    ChooseModel *chooseModel2 = self.dataSource[indexPath.row];
    cell.chooseModel2 = chooseModel2;
    if (chooseModel2.isSelected) {
        cell.isSelected = YES;
    } else {
        cell.isSelected = NO;
    }
    return cell;
}

#pragma mark --- <UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    /// 多选 判断是不是已经选中了
    ChooseCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    ChooseModel *chooseModel = self.dataSource[indexPath.row];
    if (chooseModel.isSelected) {
        [self.selectIndexPaths removeObject:chooseModel];
        chooseModel.isSelected = NO;
        cell.isSelected = NO;
    } else {
        [self.selectIndexPaths addObject:chooseModel];
        chooseModel.isSelected = YES;
        cell.isSelected = YES;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        [_tableView registerNib:[UINib nibWithNibName:@"ChooseCell" bundle:nil] forCellReuseIdentifier:@"ChooseCell"];
    }
    return _tableView;
}

- (NSArray *)dataSource
{
    if (_dataSource == nil) {
        NSArray *plistArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ChooseModel" ofType:@"plist"]];
        NSArray *modelArray = [ChooseModel mj_objectArrayWithKeyValuesArray:plistArray];
        _dataSource = [modelArray mutableCopy];
    }
    return _dataSource;
}

@end
